package com.swing.desktopapp;


import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import napkin.NapkinLookAndFeel;

public class DesktopLoginApp extends JFrame implements ActionListener {

	JDesktopPane desktopPane;
	JMenuBar menu;
	JMenu file;
	JMenu options;
	JMenu utils;

	JMenuItem createUser;
	JMenuItem login;
	JMenuItem exit;

	JMenuItem forgotPassword;
	JMenuItem adminLogin;

	JMenuItem calculator;
	JMenuItem notepad;

	JInternalFrame createUserFrame, adminFrame, forgotPasswordFrame,
			loginFrame;

	JLabel lblCreateUserLabel, lblLoginLabel, lblForgotPasswordLabel,
			lblAdminLabel, lblUserName, lblPassword, lblQuestion, lblAnswer;

	JTextField txtUserName, txtQuestion, txtAnswer;

	JPasswordField txtPassword;
	JButton btnSubmit;
	JButton btnReset;

	public DesktopLoginApp() {

		desktopPane = new JDesktopPane();
		menu = new JMenuBar();
		file = new JMenu("File");
		options = new JMenu("Options");
		utils = new JMenu("Utils");
		createUser = new JMenuItem("Create User         ");
		login = new JMenuItem("Login");
		exit = new JMenuItem("Exit");
		forgotPassword = new JMenuItem("Forgot Password    ");
		adminLogin = new JMenuItem("  Admin Login");
		calculator = new JMenuItem("Calculator         ");
		notepad = new JMenuItem("Notepad");

		Font menuFont = new Font("Verdana", Font.BOLD, 20);
		file.setForeground(Color.white);
		file.setFont(menuFont);
		file.setMnemonic('f');

		options.setForeground(Color.white);
		options.setFont(menuFont);
		options.setMnemonic('o');

		utils.setForeground(Color.white);
		utils.setMnemonic('u');
		utils.setFont(menuFont);

		menu.add(file);
		menu.add(options);
		menu.add(utils);

		Font menuItemFont = new Font("Verdana", Font.BOLD, 17);

		createUser.setBackground(Color.black);
		createUser.setForeground(Color.white);
		createUser.setFont(menuItemFont);
		createUser.setIcon(new ImageIcon("images/createUserImg.png"));
		createUser.addActionListener(this);

		login.setBackground(Color.black);
		login.setForeground(Color.white);
		login.setFont(menuItemFont);
		login.setIcon(new ImageIcon("images/userLogin.png"));
		login.addActionListener(this);

		exit.setBackground(Color.black);
		exit.setForeground(Color.white);
		exit.setFont(menuItemFont);
		exit.setIcon(new ImageIcon("images/exitImg.jpg"));
		exit.addActionListener(this);

		file.add(createUser);
		// file.addSeparator();
		file.add(login);
		// file.addSeparator();
		file.add(exit);

		forgotPassword.setBackground(Color.black);
		forgotPassword.setFont(menuItemFont);
		forgotPassword.setForeground(Color.white);
		forgotPassword.setIcon(new ImageIcon("images/forgotPasswordImg.jpg"));
		forgotPassword.addActionListener(this);

		adminLogin.setBackground(Color.black);
		adminLogin.setFont(menuItemFont);
		adminLogin.setForeground(Color.white);
		adminLogin.setIcon(new ImageIcon("images/adminLoginImg.jpg"));
		adminLogin.addActionListener(this);

		options.add(forgotPassword);
		// options.addSeparator();
		options.add(adminLogin);

		calculator.setBackground(Color.black);
		calculator.setForeground(Color.white);
		calculator.setFont(menuItemFont);
		calculator.setIcon(new ImageIcon("images/calendarImg.jpg"));
		calculator.addActionListener(this);

		notepad.setBackground(Color.black);
		notepad.setFont(menuItemFont);
		notepad.setForeground(Color.white);
		notepad.setIcon(new ImageIcon("images/notepadImg.jpg"));
		notepad.addActionListener(this);

		utils.add(calculator);
		// utils.addSeparator();
		utils.add(notepad);

		setJMenuBar(menu);
		menu.setBackground(Color.black);
		menu.setBorder(BorderFactory.createBevelBorder(5, Color.red,
				Color.black));

		add(desktopPane);
		setVisible(true);
		setTitle("Desktop Login App");

		try {
			UIManager.setLookAndFeel(new NapkinLookAndFeel());
			SwingUtilities.updateComponentTreeUI(desktopPane);
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}

		setExtendedState(JFrame.MAXIMIZED_BOTH);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

	public static void main(String[] args) {

		new DesktopLoginApp();

	}

	@Override
	public void actionPerformed(ActionEvent e) {

		Object obj = e.getSource();

		if (obj.equals(createUser)) {

			Font createUserFrameFont = new Font("Verdana", Font.BOLD, 15);

			createUserFrame = new JInternalFrame("", true, true, true, true);

			createUserFrame.setLayout(null);
			lblCreateUserLabel = new JLabel("Fill the form to get registered");
			lblCreateUserLabel.setForeground(Color.RED);
			lblCreateUserLabel.setFont(createUserFrameFont);
			lblUserName = new JLabel("Username");
			lblUserName.setFont(createUserFrameFont);
			lblPassword = new JLabel("Password");
			lblPassword.setFont(createUserFrameFont);
			lblQuestion = new JLabel("Question");
			lblQuestion.setFont(createUserFrameFont);
			lblAnswer = new JLabel("Answer");
			lblAnswer.setFont(createUserFrameFont);

			txtUserName = new JTextField();
			txtPassword = new JPasswordField();

			txtQuestion = new JTextField();
			txtAnswer = new JTextField();

			btnSubmit = new JButton("Submit");
			btnSubmit.setFont(createUserFrameFont);
			btnSubmit.addActionListener(this);
			btnReset = new JButton("Reset");

			btnReset.setFont(createUserFrameFont);

			lblCreateUserLabel.setBounds(100, 30, 300, 50);
			lblUserName.setBounds(50, 100, 150, 30);
			lblPassword.setBounds(50, 150, 150, 30);
			lblQuestion.setBounds(50, 200, 150, 30);
			lblAnswer.setBounds(50, 250, 150, 30);

			txtUserName.setBounds(200, 100, 150, 30);
			txtPassword.setBounds(200, 150, 150, 30);
			txtQuestion.setBounds(200, 200, 150, 30);
			txtAnswer.setBounds(200, 250, 150, 30);

			btnSubmit.setBounds(120, 300, 80, 40);
			btnReset.setBounds(250, 300, 80, 40);

			createUserFrame.add(lblCreateUserLabel);
			createUserFrame.add(lblUserName);
			createUserFrame.add(lblPassword);
			createUserFrame.add(lblQuestion);
			createUserFrame.add(lblAnswer);
			createUserFrame.add(txtUserName);
			createUserFrame.add(txtPassword);
			createUserFrame.add(txtQuestion);
			createUserFrame.add(txtAnswer);
			createUserFrame.add(btnSubmit);
			createUserFrame.add(btnReset);

			btnSubmit.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {

					Object obj = e.getSource();
					if (obj.equals(btnSubmit)) {

						String userName = txtUserName.getText();
						String password = txtPassword.getText();
						String question = txtQuestion.getText();
						String answer = txtAnswer.getText();

						if (userName.equals("") || password.equals("")
								|| question.equals("") || answer.equals("")) {

							JOptionPane.showMessageDialog(createUserFrame,
									"All details are mandatory",
									"Error Message", JOptionPane.ERROR_MESSAGE);

						} else {

							User user = new UserImpl();

							user.setUserName(userName);
							user.setPassword(password);
							user.setQuestion(question);
							user.setAnswer(answer);

							UserService userService = UserServiceImpl
									.getInstance();
							try {
								userService.create(user);
							} catch (IOException e1) {
								e1.printStackTrace();
							}

							JOptionPane.showMessageDialog(createUserFrame,
									"You have Registered Successfully",
									"Success Message",
									JOptionPane.INFORMATION_MESSAGE);
						}

					}

				}
			});

			btnReset.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {

					Object obj = e.getSource();
					if (obj.equals(btnReset)) {

						txtUserName.setText("");
						txtPassword.setText("");
						txtQuestion.setText("");
						txtAnswer.setText("");
						txtUserName.requestFocus();

					}

				}
			});

			createUserFrame.setFont(createUserFrameFont);
			createUserFrame.setVisible(true);
			createUserFrame.setTitle("Registration Form");
			createUserFrame.setSize(500, 500);

			desktopPane.add(createUserFrame);
		}

		if (obj.equals(login)) {

			loginFrame = new JInternalFrame("Login Frame", true, true, true,
					true);

			loginFrame.setLayout(null);

			lblLoginLabel = new JLabel("Fill The Details to Login");
			Font loginLabelFont = new Font("Verdana", Font.BOLD, 15);

			loginFrame.add(lblLoginLabel);

			lblLoginLabel.setFont(loginLabelFont);
			lblLoginLabel.setForeground(Color.red);

			lblUserName = new JLabel("Username");
			lblUserName.setFont(loginLabelFont);
			lblPassword = new JLabel("Password");
			lblPassword.setFont(loginLabelFont);
			txtUserName = new JTextField();
			txtPassword = new JPasswordField();
			btnSubmit = new JButton("Submit");
			btnReset = new JButton("Reset");

			lblLoginLabel.setBounds(100, 30, 300, 50);
			lblUserName.setBounds(50, 100, 150, 30);
			lblPassword.setBounds(50, 150, 150, 30);
			txtUserName.setBounds(200, 100, 150, 30);
			txtPassword.setBounds(200, 150, 150, 30);
			btnSubmit.setBounds(120, 200, 80, 40);
			btnReset.setBounds(250, 200, 80, 40);

			loginFrame.add(lblUserName);
			loginFrame.add(lblPassword);
			loginFrame.add(txtUserName);
			loginFrame.add(txtPassword);
			loginFrame.add(btnSubmit);
			loginFrame.add(btnReset);

			btnSubmit.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {

					Object obj = e.getSource();

					if (obj.equals(btnSubmit)) {

						String userName = txtUserName.getText();
						String password = txtPassword.getText();

						if (userName.equals("") || password.equals("null")) {

							JOptionPane.showMessageDialog(loginFrame,
									"ALL details are mandatory",
									"Error message", JOptionPane.ERROR_MESSAGE);
						}

						else {

							UserService userService = UserServiceImpl
									.getInstance();
							List<User> list = userService.getAll();

							Boolean success = false;
							for (int i = 0; i < list.size(); i++) {

								User user = list.get(i);

								if (user.getUserName().equals(userName)
										&& user.getPassword().equals(password)) {
									JOptionPane.showMessageDialog(loginFrame,
											"Login Successfull",
											"Success message",
											JOptionPane.INFORMATION_MESSAGE);
									success = true;
								}

							}
							if (success == false) {
								JOptionPane.showMessageDialog(loginFrame,
										"sorry! please check your details",
										"Error message",
										JOptionPane.ERROR_MESSAGE);

							}
						}

					}

				}
			});

			btnReset.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {

					Object obj = e.getSource();

					if (obj.equals(btnReset)) {

						txtUserName.setText("");
						txtPassword.setText("");
						txtUserName.requestFocus();

					}

				}
			});

			loginFrame.setVisible(true);
			loginFrame.setSize(400, 400);
			loginFrame.setTitle("Login Form");
			desktopPane.add(loginFrame);
		}

		if (obj.equals(exit)) {

			int choice = JOptionPane.showConfirmDialog(this, "Confirm message",
					"Are you sure you want to exit?",
					JOptionPane.OK_CANCEL_OPTION);

			if (choice == 0) {

				System.exit(0);
			}
		}

		if (obj.equals(forgotPassword)) {

			forgotPasswordFrame = new JInternalFrame("Password Recovery", true,
					true, true, true);
			Font forgotPasswordFrameFont = new Font("Verdana", Font.BOLD, 15);

			forgotPasswordFrame.setLayout(null);
			lblForgotPasswordLabel = new JLabel(
					"Fill the form to recover your password");
			lblForgotPasswordLabel.setForeground(Color.RED);
			lblForgotPasswordLabel.setFont(forgotPasswordFrameFont);
			lblUserName = new JLabel("Username");
			lblUserName.setFont(forgotPasswordFrameFont);
			lblQuestion = new JLabel("Question");
			lblQuestion.setFont(forgotPasswordFrameFont);
			lblAnswer = new JLabel("Answer");
			lblAnswer.setFont(forgotPasswordFrameFont);

			txtUserName = new JTextField();

			txtQuestion = new JTextField();
			txtAnswer = new JTextField();

			btnSubmit = new JButton("Submit");
			btnSubmit.setFont(forgotPasswordFrameFont);
			btnSubmit.addActionListener(this);
			btnReset = new JButton("Reset");

			btnReset.setFont(forgotPasswordFrameFont);

			lblForgotPasswordLabel.setBounds(100, 30, 350, 50);
			lblUserName.setBounds(50, 100, 150, 30);
			lblQuestion.setBounds(50, 150, 150, 30);
			lblAnswer.setBounds(50, 200, 150, 30);

			txtUserName.setBounds(200, 100, 150, 30);
			txtQuestion.setBounds(200, 150, 150, 30);
			txtAnswer.setBounds(200, 200, 150, 30);

			btnSubmit.setBounds(120, 250, 80, 40);
			btnReset.setBounds(250, 250, 80, 40);

			forgotPasswordFrame.add(lblForgotPasswordLabel);
			forgotPasswordFrame.add(lblUserName);
			forgotPasswordFrame.add(lblQuestion);
			forgotPasswordFrame.add(lblAnswer);
			forgotPasswordFrame.add(txtUserName);
			forgotPasswordFrame.add(txtQuestion);
			forgotPasswordFrame.add(txtAnswer);
			forgotPasswordFrame.add(btnSubmit);
			forgotPasswordFrame.add(btnReset);

			btnSubmit.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {

					Object obj = e.getSource();

					if (obj.equals(btnSubmit)) {

						String userName = txtUserName.getText();
						String question = txtQuestion.getText();
						String answer = txtAnswer.getText();

						if (userName.equals("") || question.equals("")
								|| answer.equals("")) {

							JOptionPane.showMessageDialog(forgotPasswordFrame,
									"All details are mandatory",
									"Error message", JOptionPane.ERROR_MESSAGE);
						} else {

							boolean success = false;
							UserService userService = UserServiceImpl
									.getInstance();
							List<User> list = userService.getAll();

							for (int i = 0; i < list.size(); i++) {

								User user = list.get(i);

								if (user.getUserName().equals(userName)
										&& user.getQuestion().equals(question)
										&& user.getAnswer().equals(answer)) {

									JOptionPane.showMessageDialog(
											forgotPasswordFrame,
											"Your password is "
													+ user.getPassword(),
											"Success Message",
											JOptionPane.INFORMATION_MESSAGE);

									success = true;
								}

							}
							if (success == false) {
								JOptionPane.showMessageDialog(
										forgotPasswordFrame,
										"Invalid details!", "Error message",
										JOptionPane.ERROR_MESSAGE);
							}

						}

					}

				}
			});

			btnReset.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {

					Object obj = e.getSource();

					if (obj.equals(btnReset)) {

						txtUserName.setText("");
						txtQuestion.setText("");
						txtAnswer.setText("");
						txtUserName.requestFocus();
					}
				}
			});
			forgotPasswordFrame.setFont(forgotPasswordFrameFont);
			forgotPasswordFrame.setVisible(true);
			forgotPasswordFrame.setSize(500, 400);

			desktopPane.add(forgotPasswordFrame);
		}

		if (obj.equals(adminLogin)) {

			adminFrame = new JInternalFrame("Admin Login", true, true, true,
					true);

			adminFrame.setLayout(null);

			lblAdminLabel = new JLabel("Fill The Admin Details to Login");
			Font adminLabelFont = new Font("Verdana", Font.BOLD, 15);

			adminFrame.add(lblAdminLabel);

			lblAdminLabel.setFont(adminLabelFont);
			lblAdminLabel.setForeground(Color.red);

			lblUserName = new JLabel("Username");
			lblUserName.setFont(adminLabelFont);
			lblPassword = new JLabel("Password");
			lblPassword.setFont(adminLabelFont);
			txtUserName = new JTextField();
			txtPassword = new JPasswordField();
			btnSubmit = new JButton("Submit");
			btnReset = new JButton("Reset");

			lblAdminLabel.setBounds(100, 30, 300, 50);
			lblUserName.setBounds(50, 100, 150, 30);
			lblPassword.setBounds(50, 150, 150, 30);
			txtUserName.setBounds(200, 100, 150, 30);
			txtPassword.setBounds(200, 150, 150, 30);
			btnSubmit.setBounds(120, 200, 80, 40);
			btnReset.setBounds(250, 200, 80, 40);

			adminFrame.add(lblUserName);
			adminFrame.add(lblPassword);
			adminFrame.add(txtUserName);
			adminFrame.add(txtPassword);
			adminFrame.add(btnSubmit);
			adminFrame.add(btnReset);

			btnSubmit.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {

					if (e.getSource().equals(btnSubmit)) {

						String userName = txtUserName.getText();
						String password = txtPassword.getText();

						if (userName.equals("") || password.equals("")) {

							JOptionPane.showMessageDialog(adminFrame,
									"All details are madatory!",
									"Error message", JOptionPane.ERROR_MESSAGE);
						} else if (userName.equalsIgnoreCase("admin")
								&& password.equalsIgnoreCase("admin")) {

							JOptionPane.showMessageDialog(adminFrame,
									"Welcome Admin! ", "Success message",
									JOptionPane.INFORMATION_MESSAGE);
						} else {

							JOptionPane.showMessageDialog(adminFrame,
									"Sorry! invalid details !",
									"Error message", JOptionPane.ERROR_MESSAGE);
						}

					}
				}
			});
			btnReset.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {

					Object obj = e.getSource();

					if (obj.equals(btnReset)) {

						txtUserName.setText("");
						txtPassword.setText("");
						txtUserName.requestFocus();

					}
				}
			});

			adminFrame.setVisible(true);
			adminFrame.setSize(400, 400);
			adminFrame.setTitle("Admin Form");
			desktopPane.add(adminFrame);

		}

		if (obj.equals(calculator)) {

			try {
				Runtime.getRuntime().exec("calc");
			} catch (IOException e1) {
			}
		}

		if (obj.equals(notepad)) {
			try {
				Runtime.getRuntime().exec("notepad");

			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}

	}
}
